import { defineStore } from "pinia";
import { reqC1, reqC2, reqC3 } from "@/api/product/attr";
import { ref } from "vue";
import type { CategoryResponseData, CategoryObj } from "@/api/product/attr/type";
import { ElMessage } from "element-plus";
const useCategoryStore = defineStore("Category", () => {
    // 定义一级分类数据
    const c1Arr = ref<CategoryObj[]>([]);
    // 定义二级分类数据
    const c2Arr = ref<CategoryObj[]>([]);
    // 定义三级分类数据
    const c3Arr = ref<CategoryObj[]>([]);
    // 定义一级分类id
    const c1Id = ref<number | string>("");
    // 定义二级分类id
    const c2Id = ref<number | string>("");
    // 定义三级分类id
    const c3Id = ref<number | string>("");
    const getC1 = async () => {
        const result: CategoryResponseData = await reqC1();
        if (result.code == 200) {
            c1Arr.value = result.data;
        } else {
            ElMessage.error("获取一级分类列表失败");
        }
    };
    const getC2 = async () => {
        const result: CategoryResponseData = await reqC2(c1Id.value);
        if (result.code == 200) {
            c2Arr.value = result.data;
        } else {
            ElMessage.error("获取二级分类列表失败");
        }
    };
    const getC3 = async () => {
        const result: CategoryResponseData = await reqC3(c2Id.value);
        if (result.code == 200) {
            c3Arr.value = result.data;
        } else {
            ElMessage.error("获取三级分类列表失败");
        }
    };
    // 通过三级分类id获取属性值
    return {
        getC1,
        getC2,
        getC3,
        c1Arr,
        c2Arr,
        c3Arr,
        c1Id,
        c2Id,
        c3Id,
    };
});

export default useCategoryStore;
